package com.dhu.stylefit.service;

import com.dhu.stylefit.dao.ClothesOne;
import com.dhu.stylefit.dao.ClothesOneRepository;
import com.dhu.stylefit.dto.ClothesOneDTO;
import lombok.RequiredArgsConstructor;
import org.modelmapper.ModelMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.http.HttpStatus;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class ClothesOneServiceImpl implements ClothesOneService {
    private final ClothesOneRepository clothesRepository;
    private final ModelMapper modelMapper;

    @Override
    public ClothesOneDTO createClothes(ClothesOneDTO clothesOneDTO) {
        ClothesOne clothes = modelMapper.map(clothesOneDTO, ClothesOne.class);
        ClothesOne saved = clothesRepository.save(clothes);
        return modelMapper.map(saved, ClothesOneDTO.class);
    }

    @Override
    public ClothesOneDTO getClothesById(Integer id) {
        ClothesOne clothes = clothesRepository.findById(id)
                .orElseThrow(() -> new ResponseStatusException(  HttpStatus.NOT_FOUND,"Clothes not found with id: " + id));
        return modelMapper.map(clothes, ClothesOneDTO.class);
    }

    @Override
    public List<ClothesOneDTO> getAllClothes() {
        return clothesRepository.findAll().stream()
                .map(clothes -> modelMapper.map(clothes, ClothesOneDTO.class))
                .collect(Collectors.toList());
    }

    @Override
    public List<ClothesOneDTO> getClothesByCategory(Integer categoryId) {
        return clothesRepository.findByCategoryId(categoryId).stream()
                .map(clothes -> modelMapper.map(clothes, ClothesOneDTO.class))
                .collect(Collectors.toList());
    }

    @Override
    @Transactional
    public ClothesOneDTO updateClothes(Integer id, ClothesOneDTO clothesOneDTO) {
        ClothesOne existing = clothesRepository.findById(id)
                .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND,"Clothes not found with id: " + id));

        modelMapper.map(clothesOneDTO, existing);
        ClothesOne updated = clothesRepository.save(existing);
        return modelMapper.map(updated, ClothesOneDTO.class);
    }

    @Override
    @Transactional
    public void deleteClothes(Integer id) {
        if (!clothesRepository.existsById(id)) {
            throw new ResponseStatusException(HttpStatus.NOT_FOUND,"Clothes not found with id: " + id);
        }
        clothesRepository.deleteById(id);
    }

    @Override
    public List<ClothesOneDTO> searchClothes(String keyword) {
        return clothesRepository.findByNameContaining(keyword).stream()
                .map(clothes -> modelMapper.map(clothes, ClothesOneDTO.class))
                .collect(Collectors.toList());
    }

    @Override
    public List<ClothesOneDTO> filterClothesByPrice(Float minPrice, Float maxPrice) {
        return clothesRepository.findByPriceBetween(minPrice, maxPrice).stream()
                .map(clothes -> modelMapper.map(clothes, ClothesOneDTO.class))
                .collect(Collectors.toList());
    }
}
